package com.indusfo.spc.controller;

import com.indusfo.spc.pojo.Menu;
import com.indusfo.spc.pojo.RoleMenu;
import com.indusfo.spc.service.MenuService;
import com.indusfo.spc.service.RoleMenuService;
import com.indusfo.spc.untlis.StringUtils;
import com.indusfo.spc.vo.JSONObject;
import com.indusfo.spc.vo.XCommonUtil;
import com.indusfo.spc.vo.XTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenm
 * @create 2019-11-20 15:59
 */
@Controller
@RequestMapping("/service/menu")
public class MenuController {

  @Autowired
  private MenuService menuService;

  @Autowired
  private RoleMenuService roleMenuService;

  @RequestMapping(value = "/treeMenu")
  @ResponseBody
  public JSONObject menusTree() {
    JSONObject jsonObject = new JSONObject();
    try {
      List<Menu> menuList = menuService.selectMenuAll();
      List<XTreeNode> XTreeNodes = new ArrayList<>();
      for (Menu _M : menuList) {
        XTreeNode node = new XTreeNode();
        node.setId(_M.getMenuCode());
        node.setTitle(_M.getMenuName());
        node.setOpen(true);
        node.setpId((StringUtils.isEmpty(_M.getMenuPcode())  || _M.getMenuPcode().equals("0")) ? null : _M.getMenuPcode());
        XTreeNodes.add(node);
      }
      jsonObject.setStatus(JSONObject.SUCCESS);
      jsonObject.setMsg("查询成功");
      jsonObject.setData(XCommonUtil.getTree(XTreeNodes, null));
      return jsonObject;
    } catch (Exception e) {
      e.printStackTrace();
      jsonObject.setStatus(JSONObject.ERROR);
      jsonObject.setMsg(e.getMessage());
      return jsonObject;
    }
  }

  @RequestMapping(value = "/role/treeMenu")
  @ResponseBody
  public JSONObject roleMenusTree(Integer roleId) {
    JSONObject jsonObject = new JSONObject();
    try {
      List<Menu> menuList = menuService.selectMenuAll();
      List<RoleMenu> roleMenuList = roleMenuService.selectByRoleId(roleId);
      List<String> menuCodes = roleMenuList.stream().map(RoleMenu::getMenuCode).collect(Collectors.toList());
      List<XTreeNode> XTreeNodes = new ArrayList<>();
      for (Menu _M : menuList) {
        XTreeNode node = new XTreeNode();
        node.setId(_M.getMenuCode());
        node.setTitle(_M.getMenuName());
        node.setOpen(true);
        node.setChecked(menuCodes.contains(_M.getMenuCode())&& _M.getMenuCode().length() == 6);
        node.setpId((StringUtils.isEmpty(_M.getMenuPcode())  || _M.getMenuPcode().equals("0")) ? null : _M.getMenuPcode());
        XTreeNodes.add(node);
      }
      jsonObject.setStatus(JSONObject.SUCCESS);
      jsonObject.setMsg("查询成功");
      jsonObject.setData(XCommonUtil.getTree(XTreeNodes, null));
      return jsonObject;
    } catch (Exception e) {
      e.printStackTrace();
      jsonObject.setStatus(JSONObject.ERROR);
      jsonObject.setMsg(e.getMessage());
      return jsonObject;
    }
  }
}
